<?php


namespace app\api\controller;


use think\Db;

class Functions extends Lib
{

    // 自定义
    public function zbx_function_get()
    {
        $itemids = Db::name('items')->column('itemid,deptid');

        $result = Db::connect('db2')
            ->name('functions')
            ->where('itemid','in', array_keys($itemids))
            ->field('itemid, triggerid, name')
            ->select();

        if ($result){
            foreach ($result as  $k=>&$v){
                $is_function = Db::name('functions')->where('itemid', $v['itemid'])->find();
                if($is_function){
                   unset($result[$k]);
                }else{
                    $v['deptid'] = $itemids[$v['itemid']];
                }
            }
            if($result){
                $add = Db::name('functions')->insertAll($result);
                if ($add){
                    return true;
                }else{
                    return false;
                }
            }else{
                return true;
            }
        }else{
            return true;
        }


//        return $result;
    }
}